package com.dycx.common;


import cn.hutool.core.date.LocalDateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.LocalDateTime;

@Configuration
public class SerialNumberUtil {

    private static StringRedisTemplate stringRedisTemplate;

    @Autowired
    public SerialNumberUtil(StringRedisTemplate stringRedisTemplate) {
        SerialNumberUtil.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 创建城际车订单号
     */
    public static String createIntercityOrderNo() {
        String key = "SerialNumber:Intercity:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("1");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

    /**
     * 创建班次编编
     */
    public static String createArrangeCode() {
        String key = "SerialNumber:Arrange:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("2");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

    /**
     * 创建班次段编码
     */
    public static String createArrangeSectionCode() {
        String key = "SerialNumber:ArrangeSection:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("3");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

    /**
     * 创建班次段座位编码
     */
    public static String createArrangeSectionSeatCode() {
        String key = "SerialNumber:ArrangeSectionSeat:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("4");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

    /**
     * 创建点餐订单号
     */
    public static String createFoodOrderNo() {
        String key = "SerialNumber:FoodOrderNo:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("5");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

    /**
     * 创建商城订单号
     */
    public static String createMallOrderNo() {
        String key = "SerialNumber:MallOrderNo:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("6");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

    /**
     * 创建支付订单号
     */
    public static String createPaymentOrderNo() {
        String key = "SerialNumber:PaymentOrderNo:" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
        Long value = stringRedisTemplate.opsForValue().increment(key);

        StringBuilder sb = new StringBuilder();
        sb.append("8");
        sb.append(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd"));
        sb.append(String.format("%04d", value));

        return sb.toString();
    }

}
